package com.zsrd.lessons.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsrd.lessons.domain.TsQuestion;
import com.zsrd.lessons.domain.TsQuestionBank;
import com.zsrd.lessons.domain.vo.TsQuestionBankVo;
import com.zsrd.lessons.mapper.TsQuestionBankMapper;
import com.zsrd.lessons.service.TsQuestionBankService;
import com.zsrd.lessons.service.TsQuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 题库表 服务实现类
 * </p>
 *
 * @author Chen Wei
 * @since 2022-07-11
 */
@Service
public class TsQuestionBankServiceImpl extends ServiceImpl<TsQuestionBankMapper, TsQuestionBank> implements TsQuestionBankService {
    @Autowired
    private TsQuestionService tsQuestionService;

    @Override
    public void addQuestionBank(String orgNo, String title) {
        int count = count(new QueryWrapper<TsQuestionBank>().eq("org_no", orgNo).eq("title", title));
        if (count > 0) {
            throw new RuntimeException("题目重复");
        }
        TsQuestionBank tsQuestionBank = new TsQuestionBank();
        tsQuestionBank.setTitle(title);
        tsQuestionBank.setOrgNo(orgNo);
        save(tsQuestionBank);
    }

    @Override
    public IPage<TsQuestionBankVo> getQuestionBankList(String orgNo, String loginId, Long current, Long size) {
        Page page = new Page(current, size);
        IPage<TsQuestionBankVo> list = this.baseMapper.getQuestionBankList(page, orgNo, loginId);
        return list;
    }

    @Override
    public void updateQuestionBank(Long id, String orgNo, String title) {
        TsQuestionBank byId = getById(id);
        if(StringUtils.isEmpty(byId.getOrgNo())){
            throw new RuntimeException("公共题库无法编辑或者删除");
        }
        int count = count(new QueryWrapper<TsQuestionBank>()
                .eq("org_no", orgNo)
                .eq("title", title)
                .ne("id", id));
        if (count > 0) {
            throw new RuntimeException("题目重复");
        }
        TsQuestionBank tsQuestionBank = new TsQuestionBank();
        tsQuestionBank.setId(id);
        tsQuestionBank.setTitle(title);
        tsQuestionBank.setOrgNo(orgNo);
        updateById(tsQuestionBank);
    }

    @Override
    public void deleteQuestionBank(Long id) {
        TsQuestionBank byId = getById(id);
        if(StringUtils.isEmpty(byId.getOrgNo())){
            throw new RuntimeException("公共题库无法编辑或者删除");
        }
        int count = tsQuestionService.count(new QueryWrapper<TsQuestion>().eq("bid", id));
        if (count > 0) {
            throw new RuntimeException("请先删除题库下的题目");
        }
        removeById(id);
    }

    @Override
    public List<TsQuestionBank> getQuestionBankAllList(String orgNo) {


        List<TsQuestionBank> list = this.baseMapper.getQuestionBankAllList( orgNo);
        return list;
    }
}
